package com.codedancer.campus.common.dataresource;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.codedancer.campus.common.model.xr.BaiduHotModel;
import com.codedancer.campus.common.model.xr.LocalNewsModel;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 外部Api接口数据
 */
@Component
@Slf4j
public class ExternalApiData {

    @Autowired
    private RestTemplate restTemplate;

    // 获取百度热搜接口（XR-夏柔api）
    private final String XR_BAIDU_URL = "https://zj.v.api.aa1.cn/api/baidu-rs/";
    // 本地新闻接口（XR-夏柔api）
    private final String XR_LOCAL_NEWS = "https://v.api.aa1.cn/api/api-tplist/go.php/api/News/local_news?name=";
    // 敏感词校验接口 （XR-夏柔api）
    private final String XR_SENSITIVE_WORDS = "https://v.api.aa1.cn/api/api-mgc/index.php?msg=";

    /**
     * 获取百度热搜数据
     *
     * @return
     */
    public List<BaiduHotModel> getBaiduHotData() {
        log.info("开始拉取百度热榜数据，接口地址：{}", XR_BAIDU_URL);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(XR_BAIDU_URL,
            String.class);
        JSONObject jsonObject = JSONUtil.parseObj(responseEntity.getBody());
        if (jsonObject.get("code").toString().equals("1")) {
            List<BaiduHotModel> baiduHotModels = JSONUtil.toList(jsonObject.get("data").toString(),
                BaiduHotModel.class);
            return baiduHotModels;
        }
        return null;
    }

    /**
     * 获取本地新闻
     *
     * @param location
     * @return
     */
    public List<LocalNewsModel> getLocalNewsData(String location) {
        String interfaceUrl = XR_LOCAL_NEWS + location + "&page=0";
        log.info("开始拉取本地新闻数据，接口地址：{}", interfaceUrl);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(interfaceUrl,
            String.class);
        JSONObject jsonObject = JSONUtil.parseObj(responseEntity.getBody());
        if (jsonObject.get("code").toString().equals("1")) {
            List<LocalNewsModel> localNewsModels = JSONUtil.toList(
                jsonObject.get("data").toString(),
                LocalNewsModel.class);
            return localNewsModels;
        }
        return null;
    }

    /**
     * 校验敏感词
     *
     * @param content
     * @return
     */
    public boolean checkSensitiveWords(String content) {
        String interfaceUrl = XR_SENSITIVE_WORDS + content;
        log.info("开始调用夏柔接口，接口地址为：{}", interfaceUrl);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(interfaceUrl,
            String.class);
        JSONObject jsonObject = JSONUtil.parseObj(responseEntity.getBody());
        if (jsonObject.get("code").toString().equals("200")) {
            String desc = jsonObject.get("desc").toString();
            // desc ：存在敏感词和暂无敏感词
            if (desc.equals("存在敏感词")) {
                return true;
            }
            return false;
        }
        log.info("夏柔敏感词接口调用失败");
        return true;
    }
}
